<template>
	<view>
		<view class="content">
			<view class="list">
				<view class="row" v-for="(row,index) in addressList" :key="index" @tap="select(row)">
					<view class="left">
						<view class="head">
							<image src="/static/addricon.png" mode="widthFix"></image>
						</view>
					</view>
					<view class="center">
						<view class="name-tel">
							<view class="name">{{row.name}}</view>
							<view class="tel">{{row.phone}}</view>
						</view>
						<view class="address">
							{{row.region}} {{row.address}}
						</view>
						<view class="default">
							<label class="radio">
								<radio :value="String(row.id)" :checked="row.is_default == 1"
									@click="setDefault(row)" />
								<text>默认</text>
							</label>
							<view class="setbox">
								<text class="del" @click="del(row)">删除</text>
								<text class="set" @click="edit(row)">修改</text>
							</view>
						</view>
					</view>
				</view>
			</view>
		</view>
		<view class="add">
			<view class="btn" @tap="add">
				<view class="icon tianjia"></view>新增地址
			</view>
		</view>
	</view>
</template>
<script>
	import {
		AddressList,
		AddressDefault,
		AddressDelete
	} from "@/api/api.js"
	export default {
		data() {
			return {
				isSelect: false,
				addressList: []
			};
		},
		onShow() {
			this.AddressList()
			uni.getStorage({
				key: 'delAddress',
				success: (e) => {
					let len = this.addressList.length;
					if (e.data.hasOwnProperty('id')) {
						for (let i = 0; i < len; i++) {
							if (this.addressList[i].id == e.data.id) {
								this.addressList.splice(i, 1);
								break;
							}
						}
					}
					uni.removeStorage({
						key: 'delAddress'
					})
				}
			})
			uni.getStorage({
				key: 'saveAddress',
				success: (e) => {
					let len = this.addressList.length;
					if (e.data.hasOwnProperty('id')) {
						for (let i = 0; i < len; i++) {
							if (this.addressList[i].id == e.data.id) {
								this.addressList.splice(i, 1, e.data);
								break;
							}
						}
					} else {
						let lastid = this.addressList[len - 1];
						lastid++;
						e.data.id = lastid;
						this.addressList.push(e.data);
					}
					uni.removeStorage({
						key: 'saveAddress'
					})
				}
			})
		},
		onLoad(e) {
			if (e.type == 'select') {
				this.isSelect = true;
			}
		},
		methods: {
			del(row) {
				var that = this
				uni.showModal({
					title: '温馨提示',
					content: '确认删除该地址?',
					success(res) {
						if (res.confirm) {
							AddressDelete({
								id: row.id
							}).then(res => {
								uni.showToast({
									title: res.msg,
									icon: 'none'
								})
								if (res.code == 0) {
									setTimeout(() => {
										that.AddressList()
									}, 500)
								}
							})
						}
					}
				})
			},
			setDefault(row) {
				AddressDefault({
					id: row.id
				}).then(res => {
					if (res.code == 0) {
						this.AddressList()
					} else {
						uni.showToast({
							title: res.msg,
							icon: 'none'
						})
					}
				})
			},
			AddressList() {
				AddressList().then(res => {
					this.addressList = res.data
				})
			},
			edit(row) {
				uni.setStorage({
					key: 'address',
					data: row,
					success() {
						uni.navigateTo({
							url: "edit/edit?type=edit"
						})
					}
				});

			},
			add() {
				uni.navigateTo({
					url: "edit/edit?type=add"
				})
			},
			select(row) {
				//是否需要返回地址(从订单确认页跳过来选收货地址)
				if (!this.isSelect) {
					uni.setStorage({
						key: 'address',
						data: row,
						success() {
							uni.navigateTo({
								url: "edit/edit?type=edit"
							})
						}
					});
					return;
				}
				let pages = getCurrentPages(); //获取所有页面栈实例列表
				let nowPage = pages[pages.length - 1]; //当前页页面实例
				let prevPage = pages[pages.length - 2]; //上一页页面实例
				prevPage.$vm.recinfo = row; //修改上一页data里面的tagIndex 参数值
				uni.navigateBack({ //uni.navigateTo跳转的返回，默认1为返回上一级
					delta: 1
				});
				// uni.setStorage({
				// 	key: 'address',
				// 	data: row,
				// 	success(res) {
				// 		uni.navigateBack()
				// 	}
				// })

			}
		}
	}
</script>

<style lang="scss">
	view {
		display: flex;
	}

	page {
		padding-top: 10px;
		background: #f3f3f3;
	}

	.icon {
		// &.bianji {
		// 	&:before{content:"\e61b";}
		// }
		// &.tianjia {
		// 	&:before{content:"\e81a";}
		// }
	}

	.add {
		position: fixed;
		bottom: 0;
		width: 100%;
		height: 120upx;
		justify-content: center;
		align-items: center;
		background: #fff;
		max-width: 500px;

		.btn {
			// box-shadow: 0upx 5upx 10upx rgba(0, 0, 0, 0.4);
			width: 90%;
			height: 80upx;
			border-radius: 80upx;
			background-color: #FC6512;
			color: #fff;
			justify-content: center;
			align-items: center;

			.icon {
				height: 80upx;
				color: #fff;
				font-size: 30upx;
				justify-content: center;
				align-items: center;
			}

			font-size: 30upx;
		}
	}

	.list {
		background: #fff;
		border-radius: 8px;
		flex-wrap: wrap;
		align-items: center;
		width: 96vw;
		margin: 0 auto;
		box-sizing: border-box;
		max-width: 500px;

		.row {
			padding: 28upx 5% 28upx 3%;
			display: flex;
			justify-content: space-between;
			width: 100%;
			max-width: 500px;
			box-sizing: border-box;

			.left {
				width: 90upx;
				flex-shrink: 0;
				align-items: center;

				.head {
					width: 60upx;
					height: 60upx;
					color: #fff;
					justify-content: center;
					align-items: center;
					border-radius: 60upx;
					font-size: 25upx;

					image {
						width: 100%;
						height: 100%;
					}
				}
			}

			.center {
				width: 100%;
				flex-wrap: wrap;

				.name-tel {
					width: 100%;
					align-items: baseline;

					.name {
						font-size: 30upx;
					}

					.tel {
						margin-left: 20upx;
						font-size: 28upx;
						color: #777;
					}

					.default {
						font-size: 22upx;
						color: #FC6512;
						border-radius: 24upx;
						margin-left: 20upx;
					}
				}

				.default {
					font-size: 12px;
					margin-top: 10upx;
					display: flex;
					justify-content: space-between;
					align-items: center;
					width: 100%;

					radio {
						transform: scale(.8);
					}

					.setbox {
						.set {
							margin-left: 20upx;
						}

						.del {
							color: red;
						}
					}
				}

				.address {
					width: 100%;
					font-size: 24upx;
					align-items: baseline;
					color: #777;
					margin-top: 5px;
				}
			}

			.right {
				flex-shrink: 0;
				align-items: center;
				margin-left: 20upx;

				image {
					width: 35upx;
				}

				.icon {
					justify-content: center;
					align-items: center;
					width: 80upx;
					height: 60upx;
					border-left: solid 1upx #aaa;
					font-size: 40upx;
					color: #777;
				}
			}
		}
	}
</style>